package com.example.testapp.activity.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.example.testapp.R;
import com.example.testapp.activity.model.BgPicEntity;
import com.example.testapp.activity.util.SharePreferenceUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by l on 2016/8/2.
 */
public class ChangeBackgroundFragment extends Fragment {

    private View baseView;
    private List<BgPicEntity> mPicList;
    private GridView  mGridView;
    private BgPicGridAdapter  mAdapter;
    private SharePreferenceUtil  shareUtil;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        baseView = inflater.inflate(R.layout.fragment_change_background, container, false);
        initBackgroundPic();
        initView();
        return  baseView;
    }

    private void initBackgroundPic() {
        AssetManager manager = getActivity().getAssets();
        try {
            String[] drawableList = manager.list("bkgs");
            mPicList = new ArrayList<>();
            for (String path : drawableList){
                BgPicEntity entity = new BgPicEntity();
                InputStream is = manager.open("bkgs/" + path);
                Bitmap bitmap = BitmapFactory.decodeStream(is);
                entity.path = path;
                entity.bitmap = bitmap;
                mPicList.add(entity);
                is.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void initView() {
        mGridView = (GridView)baseView.findViewById(R.id.change_background_grid);
        mAdapter = new BgPicGridAdapter(getActivity(), mPicList);
        mGridView.setAdapter(mAdapter);

        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String path = ((BgPicEntity)mAdapter.getItem(position)).path;
                shareUtil = new SharePreferenceUtil(getActivity());
                shareUtil.savePicPath(path);
                Log.i("weatherIndex",path);

                Intent intent = new Intent("change_background");
                intent.putExtra("path", path);
                getActivity().sendBroadcast(intent);
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    public class BgPicGridAdapter extends BaseAdapter{

        private List<BgPicEntity>  mPicList;
        private Resources  resources;
        private Context    mContext;
        private String mDefaultBgPath;
        private SharePreferenceUtil sharePrefrenceUtil;

        public BgPicGridAdapter(Context context, List<BgPicEntity> mPicList) {
            this.mContext = context;
            this.mPicList = mPicList;
            this.resources = mContext.getResources();
            sharePrefrenceUtil = new SharePreferenceUtil(mContext);
        }

        @Override
        public int getCount() {
            return mPicList.size();
        }

        @Override
        public Object getItem(int position) {
            return mPicList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if(convertView == null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.view_back_grid_item, parent, false);
                holder.checkedIv = (ImageView)convertView.findViewById(R.id.gridview_item_checked_iv);
                holder.backgroundIv = (ImageView)convertView.findViewById(R.id.gridview_item_iv);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

            holder.backgroundIv.setBackgroundDrawable(new BitmapDrawable(resources, ((BgPicEntity)getItem(position)).bitmap));

            mDefaultBgPath = sharePrefrenceUtil.getPath();
            if(((BgPicEntity)getItem(position)).path.equals(mDefaultBgPath)){
                holder.checkedIv.setVisibility(View.VISIBLE);
            }else {
                holder.checkedIv.setVisibility(View.GONE);
            }

            return convertView;
        }

        public class ViewHolder{
             ImageView checkedIv, backgroundIv;
        }
    }
}
